package com.mdlib.helper.module.send.profile.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.event.AddressEvent;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.AddressEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.AddressAdapter;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/15 17:17
 * 邮箱：hqy_xz@126.com
 */

public class AddressFragment extends BaseTitleFragment {

    @Bind(R.id.ll_address_null)
    LinearLayout mLlAddressNull;
    @Bind(R.id.rv_address)
    RecyclerView mRvAddress;
    @Bind(R.id.tv_new_add_address)
    TextView mTvNewAddAddress;
    @Bind(R.id.ll_new_add_address)
    LinearLayout mLlNewAddAddress;
//    @Bind(R.id.sv_address_list)
//    SpringView mSvAddressList;

    private boolean isHide = false;

    private AddressAdapter mAddressAdapter;

    private List<AddressEntity> addressList = new ArrayList<>();

    private String type;

    public static AddressFragment newInstance(String type) {
        AddressFragment fragment = new AddressFragment();
        Bundle bundle = new Bundle();
        bundle.putString(UIHelper.TYPE, type);
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {
            getActivity().finish();
        }
        type = getArguments().getString(UIHelper.TYPE);

    }

    @Override
    public void onStart() {
        super.onStart();
        getAddressList();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_address;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.me_tv_address, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                })
                .setTvRightBg(R.string.address_right_tv_edit)
                .setImgRightListener(mEditClickListener);
        init();
    }

    public void init() {
        mAddressAdapter = new AddressAdapter(addressList);
        mRvAddress.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvAddress.setAdapter(mAddressAdapter);
        mRvAddress.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);
                if (type.equals(UIHelper.ADDRESS_TYPE_ONE)) {
                    EventBus.getDefault().post(new AddressEvent(addressList.get(position).getAddress()));
                    getActivity().finish();
                }
            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                switch (view.getId()) {
                    case R.id.iv_address_delete:
                        deteleAddress(addressList.get(position).getAddressId());
                        break;
                    case R.id.iv_address_edit:
                        addFragment(AddressRelateFragment.newInstance(addressList.get(position)));
                        break;
                }

            }
        });
//        mSvAddressList.setType(SpringView.Type.FOLLOW);
//        mSvAddressList.setListener(new SpringView.OnFreshListener() {
//            @Override
//            public void onRefresh() {
//                getAddressList();
//            }
//
//            @Override
//            public void onLoadmore() {
//
//            }
//        });
//        mSvAddressList.setHeader(new HelperHeads(getActivity()));
        getAddressList();
    }

    /**
     * 获取地址列表
     */
    private void getAddressList() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi().getAddressList()
                .compose(RxHelper.<ArrayList<AddressEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<AddressEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<AddressEntity> addressEntities) {
                        stopLoadingDialog();
                        addressList = addressEntities;
                        if (addressList.size() <= 0) {
                            mLlAddressNull.setVisibility(View.VISIBLE);
                            mLlNewAddAddress.setVisibility(View.GONE);
                        } else {
                            mLlAddressNull.setVisibility(View.GONE);
                            mLlNewAddAddress.setVisibility(View.VISIBLE);
                        }
                        mAddressAdapter.setNewData(addressList);
                        mAddressAdapter.notifyDataSetChanged();
//                        mSvAddressList.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
//                        mSvAddressList.onFinishFreshAndLoad();
                    }
                }));
    }

    private void deteleAddress(int id) {
        addSubscribe(DataManager.getInstance().getUserApi()
                .deteleAddrss(String.valueOf(id))
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        ToastUtil.showToast(baseResult.msg);
                        showIsHide();
                        getAddressList();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);

                    }
                }));

    }


    public View.OnClickListener mEditClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showIsHide();
        }
    };

    @OnClick(R.id.tv_new_add_address)
    public void onClick() {
        addFragment(AddressRelateFragment.newInstance());
    }

    private void showIsHide() {
        if (isHide == false) {
            for (int i = 0; i < addressList.size(); i++) {
                addressList.get(i).setHide(true);
            }
            isHide = true;
        } else if (isHide == true) {
            for (int i = 0; i < addressList.size(); i++) {
                addressList.get(i).setHide(false);
            }
            isHide = false;
        }
        mAddressAdapter.notifyDataSetChanged();
    }

}
